package web.controller;

import hibernate.pojo.User;
import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;


public class ListUsersController {

	
	private static final Log logger = LogFactory.getLog(LoginController.class);
	
	private List<User> users;
	private User user;
	private UIData usersTable;
	private String firstName;
	private String lastName;
	private String userName;
	
	public ListUsersController() {
		// TODO Auto-generated constructor stub
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public List<User> getUsers() {
		
		try {
			users = new ArrayList<User>();
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(User.class);
			users = criteria.list();
		} catch (Exception exception) {
			logger.error("Kullanici listesi database den okunurken hata olustu!",exception);
		}
		
		if(users != null && users.size() > 0){
			return users;
		} else 
			return null;
	}
	
	public String goruntule(){
		String pageToReturn = "";
		try {
			setUser(((User)usersTable.getRowData()));
			setFirstName(getUser().getFirstName());
			setLastName(getUser().getLastName());
			setUserName(getUser().getUserName());
			
			logger.info("Initial User Values!");
			logger.info(getFirstName());
			logger.info(getLastName());
			logger.info(getUserName());
			pageToReturn = "showUserDetailPage";
		} catch (Exception exception) {
			logger.error("Secilen kullaniciyi getirirken hata olustu!", exception);
		}
		
		return pageToReturn;
		
	}
	
	public String updateUserInformation(){
		String pageToRedirect = "";
		
		logger.info("Updated User Values!");
		logger.info(getFirstName());
		logger.info(getLastName());
		logger.info(getUserName());
		
		try {
			
//			FacesContext context = FacesContext.getCurrentInstance();
//			user = (User)context.getExternalContext().getSessionMap().get("user");
			user.setFirstName(getFirstName());
			user.setLastName(getLastName());
			user.setUserName(getUserName());
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(user);
			transaction.commit();
		
			pageToRedirect = "updateUserInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}

	public void setUsersTable(UIData usersTable) {
		this.usersTable = usersTable;
	}

	public UIData getUsersTable() {
		return usersTable;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
	
	
	
	
}
